package com.jcrj.service;

import com.jcrj.dao.OrganizationDao;
import com.jcrj.entity.OOrg;
import com.jcrj.entity.OUser;
import com.jcrj.entity.OWorkGroup;
import java.util.List;

public class OrganizationService {
	private OrganizationDao organizationDao;
	private StringBuffer buff;

	public OrganizationDao getOrganizationDao() {
		return this.organizationDao;
	}

	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

	public String getAllTree() {
		StringBuffer sb = new StringBuffer();
		buff = new StringBuffer();
		String hql = "from OOrg o where o.pid=0 and  o.del='0' order by o.px asc";
		List list = this.organizationDao.findByHql(hql);
		sb.append("[");
		for (int i = 0; i < list.size(); i++) {
			OOrg org = (OOrg) list.get(i);
			sb.append("{");
			sb.append("\"id\": \"" + org.getId() + "\", \"text\": \""
					+ org.getName()
					+ "\", \"iconCls\": \"icon-ok\", \"state\": \"open\"");
			String hql1 = "from OOrg o where o.pid=" + org.getId()
					+ " and  o.del='" + "0" + "' order by o.px asc";
			List list1 = this.organizationDao.findByHql(hql1);
			if ((list1 != null) && (!list1.isEmpty())) {
				sb.append(",");
				sb.append("\"children\"").append(":");
				sb.append("[");
			}
			for (int j = 0; j < list1.size(); j++) {
				OOrg org1 = (OOrg) list1.get(j);
				sb.append("{");
				sb.append("\"id\": \"" + org1.getId()
						+ "\", \"text\": \"" + org1.getName()
						+ "\", \"state\": \"closed\"");
				sb.append("}");
				if (j != list1.size() - 1) {
					sb.append(",");
				}
			}
			if ((list1 != null) && (!list1.isEmpty())) {
				sb.append("]");
			}
			sb.append("}");
			if (i != list.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]");
		return sb.toString();
	}

	private String StringFormat(OOrg org) {
		this.buff.append("{");
		this.buff.append("\"id\": \"" + org.getId() + "\", \"text\": \""
				+ org.getName()
				+ "\", \"iconCls\": \"icon-ok\", \"state\": \"closed\"");
		this.buff.append("}");
		return this.buff.toString();
	}

	private String StringGroupFormat(OWorkGroup group) {
		this.buff.append("{");
		this.buff.append("\"id\": \"" + group.getId() + "\", \"text\": \""
				+ group.getName()
				+ "\", \"iconCls\": \"icon-ok\", \"state\": \"closed\"");
		this.buff.append("}");
		return this.buff.toString();
	}

	public String getChildTree(int id) {
		this.buff = new StringBuffer();
		String hql = "from OOrg o where o.pid=" + id + " and  o.del='" + "0"
				+ "' order by o.px asc";
		List list = this.organizationDao.findByHql(hql);
		this.buff.append("[");
		for (int i = 0; i < list.size(); i++) {
			OOrg org = (OOrg) list.get(i);
			StringFormat(org);
			if (i != list.size() - 1) {
				this.buff.append(",");
			}
		}
		this.buff.append("]");
		return this.buff.toString();
	}

	public List<OOrg> getOrgListById(int pid, int currentPage) {
		String hql = "from OOrg o where o.del='0' and  o.pid=" + pid
				+ " order by o.px asc";
		return this.organizationDao.findByPage(hql, currentPage);
	}

	public Long getTotalCount(int orgId) {
		String hql = "select count(id) from OOrg where del='0' and  pid="
				+ orgId;
		return this.organizationDao.countTotalByHql(hql);
	}

	public List<OOrg> findByName(String name, String pid) {
		String hql = "from OOrg o where o.del='0'";
		if ((pid != null) && (!"".equals(pid))) {
			hql = hql + " and o.pid=" + pid + " ";
		}
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and o.name like '%" + name + "%'";
		}
		return this.organizationDao.findByHql(hql);
	}

	public OOrg getOrganization(int id) {
		return (OOrg) this.organizationDao.getObject(new OOrg(), id);
	}

	public void saveOrg(OOrg org) {
		this.organizationDao.saveOrg(org);
	}

	public void updateOrg(OOrg org) {
		this.organizationDao.updateOrg(org);
	}

	public void delete(Integer orgId) {
		String hql = "update OOrg set del='1' where id=" + orgId;
		this.organizationDao.executeHql(hql);
	}

	public String getWorkGroup(int userId) {
		this.buff = new StringBuffer();
		String hql = "from OWorkGroup where userid=" + userId;
		List list = this.organizationDao.findByHql(hql);
		this.buff.append("[");
		for (int i = 0; i < list.size(); i++) {
			OWorkGroup group = (OWorkGroup) list.get(i);
			StringGroupFormat(group);
			if (i != list.size() - 1) {
				this.buff.append(",");
			}
		}
		this.buff.append("]");
		return this.buff.toString();
	}

	public List<OUser> gteGroupUserList(int groupId) {
		String hql = "from OUser o where o.id in(select userId from OGroupUser where groupId="
				+ groupId + ")";
		return this.organizationDao.findByHql(hql);
	}
}